package com.ks.m.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ks.code.ClassifyCode;
import com.ks.dto.BrandDto;
import com.ks.dto.ClassifyDto;
import com.ks.dto.OtherDto;
import com.ks.dto.PaginationDto;
import com.ks.dto.ProductDto;
import com.ks.pc.service.CommonService;
import com.ks.pc.service.SysOtherService;
import com.ks.pc.service.SysProductService;

@Controller
@RequestMapping("/m")
public class MHomeController {

	@Autowired
	private SysProductService sysProductService;
	@Autowired
	private SysOtherService sysOtherService;
	@Autowired
	private CommonService commonService;
	
	
	@RequestMapping(value = "/mHome", method = RequestMethod.GET)
	public String mHomeView(HttpServletRequest request, Model model) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("pageStart", 0);
		param.put("pageSize", 4);
		param.put("classifyId", ClassifyCode.PHONE);
		List<ProductDto> phoneDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.IPAD);
		List<ProductDto> padDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.CAMERA);
		List<ProductDto> cameraDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.PC);
		List<ProductDto> pcDtos = sysProductService.findProductByClassifyId(param);

		List<String> bannerImgPaths = sysProductService.findBannerById(0);	//首页横幅
		model.addAttribute("bannerImgPaths", bannerImgPaths);
		
		HttpSession session = request.getSession();
		@SuppressWarnings("unchecked")
		List<ClassifyDto> classifyList = (List<ClassifyDto>)session.getAttribute("classifyDto");
		if(null == classifyList){
			classifyList = sysProductService.findClassifyDto();
			for (ClassifyDto classifyDto : classifyList) {
				if(classifyDto.getId().equals(ClassifyCode.PHONE)){
					classifyDto.setProductList(phoneDtos);
					continue;
				}
				if(classifyDto.getId().equals(ClassifyCode.CAMERA)){
					classifyDto.setProductList(cameraDtos);
				}
				if(classifyDto.getId().equals(ClassifyCode.PC)){
					classifyDto.setProductList(pcDtos);
					continue;
				}
				if(classifyDto.getId().equals(ClassifyCode.IPAD)){
					classifyDto.setProductList(padDtos);
					continue;
				}
			}
			session.setAttribute("classifyList", classifyList);
		}
		return "m/mHome";
	}

	@RequestMapping(value = "/productList", method = RequestMethod.GET)
	public String productListView(Model model, Integer classifyId, Integer brandId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("pageSize", 9999);
		param.put("classifyId", classifyId);
		param.put("brandId", brandId);
		List<ProductDto> productDtos = 
				sysProductService.findProductByClassifyId(param);
		model.addAttribute("productDtos", productDtos);
		
		PaginationDto<BrandDto> dto = new PaginationDto<BrandDto>(9999, 9999);
		RowBounds rowBounds = new RowBounds(dto.getOffset(), dto.getLimit());
		param.put("rowBounds", rowBounds);
		
		
		 List<BrandDto> rows = commonService.queryBrandByCondition(classifyId, 9999, 9999, null).getRows();
		 
		dto = sysProductService.findBrandGrid(dto, param);
		
		
		model.addAttribute("brandDtos", dto.getRows());
		model.addAttribute("classifyId", classifyId);
		
		List<String> bannerImgPaths = sysProductService.findBannerById(classifyId);	//首页横幅
		model.addAttribute("bannerImgPaths", bannerImgPaths);
		
		return "m/productList";
	}
	
	
	@RequestMapping(value = "/product", method = RequestMethod.GET)
	public String productView(Model model, Integer productId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("productId", productId);
		ProductDto productDto = 
				sysProductService.findProductById(productId);
		model.addAttribute("productDto", productDto);
		return "m/product";
	}
	
	@RequestMapping(value = "/catalog", method = RequestMethod.GET)
	public String catalogView(Model model, Integer productId) {
		return "m/catalog";
	}
	
	@RequestMapping(value = "/about", method = RequestMethod.GET)
	public String aboutView(Model model) {
		OtherDto otherDto = sysOtherService.findOtherById(1);
		model.addAttribute(otherDto);
		return "m/about";
	}
	

	@RequestMapping(value = "/mOnlineOpt", method = RequestMethod.GET)
	public String mOnlineOpt(Model model) {
		return "m/mOnlineOpt";
	}
	
}
